import ttkbootstrap as ttk
from code.util.db import sqlite_util as sqlite_db


def pop_info():
    # 创建一个新窗口作为弹出窗口
    '''
        cosmo
        litera
        superhero
        flatly
    '''
    # popup = ttk.Window(themename="litera")
    popup = ttk.Toplevel()
    popup.title("Popup Window")
    popup.geometry("400x400")

    '''连接数据库'''
    res_list = sqlite_db.select("select * from tb_info")
    res = res_list[0]
    print(type(res))
    print(res)


    info_frame = ttk.Frame(popup)
    info_frame.pack()
    # 将第一行数据转为对象
    # tb_info = entity.DBInfo(*res)
    # print(tb_info.host)
    entry_map = {}
    colume_name_list = ["host:","用户名:","密码:","目标数据库:"]
    for index, value in enumerate(res):
        info_index = index - 1
        info_name = colume_name_list[info_index]
        if index == 0:
            entry_map[id] = value
            pass
        else:
            host = ttk.Label(info_frame, text=info_name)
            host.grid(row=info_index, column=0)

            # host 输入框
            host_entry = ttk.Entry(info_frame)
            host_entry.insert(0, value)  # 手动将默认值插入输入框
            host_entry.grid(row=info_index, column=1, padx=10, pady=10)
            print(f"info_name : {value}")
            entry_map[info_index] = host_entry

    button_save = ttk.Button(popup, text="保存", command=lambda: save_info(entry_map))
    button_save.pack()

    # 添加一个按钮，用于关闭弹出窗口
    button = ttk.Button(popup, text="关闭", command=popup.destroy)
    button.pack(pady=20)

def save_info(entry_map):
    print(f"id:{entry_map[id]}")
    sqlite_db.update(f"update tb_info set host='{entry_map[0].get()}'"
              f", user_name = '{entry_map[1].get()}'"
              f", password = '{entry_map[2].get()}'"
              f", database = '{entry_map[3].get()}'"
              f" where id = 1"
         )








